/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode cur = new ListNode(-1);
        ListNode curCur = cur;
        int s = 0;
        while(l1 != null || l2 != null) {
            if(l1 == null) {
                if(s == 0) {
                    curCur.next = l2;
                    break; 
                } else {
                    int hh = l2.val;
                    s = (hh+1)/10;
                    hh = (hh+1)%10;
                    ListNode q = new ListNode(hh);
                    curCur.next = q;
                    curCur = curCur.next;
                    l2 = l2.next;
                }
                continue;
            }
            if(l2 == null) {
                if(s == 0) {
                    curCur.next = l1;
                    break; 
                } else {
                    int hh = l1.val;
                    s = (hh+1)/10;
                    hh = (hh+1)%10;
                    ListNode q = new ListNode(hh);
                    curCur.next = q;
                    curCur = curCur.next;
                    l1 = l1.next;
                }
                continue;
            }
            int he = l1.val + l2.val;
            if(s == 1) {
                he = he + 1; 
            }
            s = he/10;
            he = he%10;
            ListNode curNew = new ListNode(he);
            curCur.next = curNew;
            curCur = curCur.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        if(s == 1) {
            ListNode curNew = new ListNode(1);
            curCur.next = curNew;
            curCur = curCur.next;
        }
        return cur.next;
    }
}